package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlc.felear.xiaoensale.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ShowPicAdapter extends BaseAdapter {

    private static final String TAG = "ShowPicAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<String> items;
    public String picDirPath= Environment.getExternalStorageDirectory() + "/zkyc/pic/";

    public ShowPicAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
    }

    public List<String> getData() {
        return items;
    }


    public void setItems(List<String> data) {
        if (data.isEmpty()) {
            notifyDataSetChanged();
            return;
        }
        Collections.sort(data);
        items.clear();
        items.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public String getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_menu_pic_item, null);
            holder.iv = convertView.findViewById(R.id.iv_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(picDirPath+items.get(position)).into(holder.iv);
        return convertView;
    }

    public void removeData(String tempName) {
        items.remove(tempName);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tv_name;
        ImageView iv;
    }

}